#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>

using namespace std;

template<class T>
class Shared_ptr
{
public:
	Shared_ptr(T* ptr)
		:_ptr(ptr)
		,_count(nullptr)
	{
		_count = new int(1);
	}
	~Shared_ptr()
	{
		if ((*_count)-- == 0) delete _ptr;
	}
	Shared_ptr(Shared_ptr<T>& sp)
		:_ptr(sp._ptr)
		,_count(sp._count)
	{
		(*_count)++;
	}
	Shared_ptr<T>& operator =(Shared_ptr<T>& sp)
	{
		if (sp._ptr != _ptr)
		{
			if (((*_count)--) == 0 && _ptr)
			{
				delete _ptr;
				delete _count;
			}
			_ptr = sp._ptr;
			_count = sp._count;
			(*_count)++;
		}
		return *this;
	}
private:
	T* _ptr;
	int* _count;
};

int main()
{
	Shared_ptr<int> sp1(new int(1));
	//Shared_ptr<int> sp2(sp1);
	//sp1 = sp2;
	Shared_ptr<int> sp3(new int(2));
	sp1 = sp3;
	return 0;
}